
package dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.GregorianCalendar;

import DBM.DBManager;
import bean.ActionBEAN;

public class ActionDAO {
	private final String	getActionsById	= "SELECT * FROM Action WHERE characterID = ?";
	private final String	doSimpleCrime	= "UPDATE Action SET simpleCrime = ? WHERE characterID = ?";
	private final String	doBigCrime		= "UPDATE Action SET bigCrime = ? WHERE characterID = ?";
	private final String	doShootArea		= "UPDATE Action SET shootArea = ? WHERE characterID = ?";
	private final String	inJail			= "UPDATE Action SET jail = ? WHERE characterID = ?";
	private final String	addAction		= "INSERT INTO Action VALUES( ?, ?, ?, ?, ?, ? )";
	private DBManager		dbmanager		= null;

	public ActionDAO( DBManager DBM ) {
		this.dbmanager = DBM;
	}

	public ActionBEAN getActionsByID( int accountID ) {
		ActionBEAN action = null;
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( getActionsById );
			ppstm.setInt( 1, accountID );
			ResultSet res = ppstm.executeQuery();
			while( res.next() ) {
				action = new ActionBEAN( res.getInt( "characterID" ), res
						.getString( "jail" ), res.getString( "simpleCrime" ),
						res.getString( "shootArea" ), res
								.getString( "bigCrime" ) );
			}
		} catch( SQLException e ) {
			e.printStackTrace();
		}
		return action;
	}

	public void doSimpleCrime( int characterID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( doSimpleCrime );
			ppstm.setString( 1, getSimpleCrimeEndtime() );
			ppstm.setInt( 2, characterID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void doBigCrime( int characterID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( doBigCrime );
			ppstm.setString( 1, getBigCrimeEndtime() );
			ppstm.setInt( 2, characterID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void doShootArea( int characterID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( doShootArea );
			ppstm.setString( 1, getSimpleCrimeEndtime() );
			ppstm.setInt( 2, characterID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void inJail( int characterID, int level ) {
		try {
			PreparedStatement ppstm = dbmanager.makePreparedStatement( inJail );
			ppstm.setString( 1, getJailEndtime( level ) );
			ppstm.setInt( 2, characterID );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	public void addAction( int newID ) {
		try {
			PreparedStatement ppstm = dbmanager
					.makePreparedStatement( addAction );
			ppstm.setInt( 1, newID );
			ppstm.setInt( 2, newID );
			ppstm.setString( 3, "2012-01-22 12:00:00" );
			ppstm.setString( 4, "2012-01-22 12:00:00" );
			ppstm.setString( 5, "2012-01-22 12:00:00" );
			ppstm.setString( 6, "2012-01-22 12:00:00" );
			ppstm.execute();
		} catch( SQLException e ) {
			e.printStackTrace();
		}
	}

	/*
	 * from here there are only math based methods
	 */
	public String getStringFromCalendar( Calendar time ) {
		if( time == null )
			return "";
		SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" );
		return sdf.format( time.getTime() );
	}

	private String getJailEndtime( int level ) {
		Calendar cal = new GregorianCalendar();
		// we should make this better, anyway it works ^^ don't fix what aint
		// broken!
		cal.add( Calendar.MINUTE, level + 1 );
		String endTime = getStringFromCalendar( cal );
		System.out.println( endTime );
		return endTime;
	}

	private String getSimpleCrimeEndtime() {
		Calendar cal = new GregorianCalendar();
		cal.add( Calendar.MINUTE, 2 );
		String endTime = getStringFromCalendar( cal );
		System.out.println( endTime );
		return endTime;
	}

	private String getBigCrimeEndtime() {
		Calendar cal = new GregorianCalendar();
		cal.add( Calendar.MINUTE, 5 );
		String endTime = getStringFromCalendar( cal );
		System.out.println( endTime );
		return endTime;
	}
}
